package com.bjpowernode.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjpowernode.shop.base.BaseStore;
import com.bjpowernode.shop.constants.CommonConstants;
import com.bjpowernode.shop.domain.HotSearch;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/admin/hotSearch")
public class HotSearchController extends BaseStore {

    @GetMapping("/page")
    public ResponseEntity<Page<HotSearch>> page(Page<HotSearch> page,
                                                String content,
                                                String title,
                                                Integer status) {
        return ResponseEntity.ok(
                hotSearchService.page(
                        page,
                        new LambdaQueryWrapper<HotSearch>()
                                .eq(ObjectUtils.isNotEmpty(status), HotSearch::getStatus, status)
                                .like(StringUtils.isNotEmpty(content),HotSearch::getContent,content)
                                .like(StringUtils.isNotEmpty(title),HotSearch::getTitle,title)
                ));
    }

    @PostMapping
    @Transactional
    public ResponseEntity<Boolean> save(@RequestBody HotSearch hotSearch) {
        return ResponseEntity.ok(hotSearchService.save(
                hotSearch.setShopId(CommonConstants.DEFAULT_SHOP_ID)
                        .setRecDate(new Date())
        ));
    }
}
